package com.example.nophase.activity.manager;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import com.example.nophase.R;
import com.example.nophase.adapter.TaskListActivityAdapter;
import com.example.nophase.api.RequstCommonApi;
import com.example.nophase.base.BaseActivity;
import com.example.nophase.common.BaseCallBack;
import com.example.nophase.common.BaseResp;
import com.example.nophase.common.LoginManager;
import com.example.nophase.entity.AdultItemDTO;
import com.example.nophase.entity.SysUserVO;
import com.example.nophase.utils.HttpUtils;

import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Response;

public class AdultListActivity extends BaseActivity {
    @BindView(R.id.iv_background)
    ImageView ivBackground;
    private List<AdultItemDTO> resultListBeen = new ArrayList<>();

    TaskListActivityAdapter adapter;
    @BindView(R.id.home_toobar)
    Toolbar homeToobar;
    @BindView(R.id.applyfor_lv)
    ListView applyforLv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_adult_list);
        ButterKnife.bind(this);
        setToolbar();
        initData();

    }

    private void setToolbar() {
        homeToobar.setNavigationIcon(R.drawable.ic_left_black_24dp);
        homeToobar.setTitle("");
        setSupportActionBar(homeToobar);
        getSupportActionBar().setHomeButtonEnabled(true);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

            case android.R.id.home:
                this.finish();
                return true;

        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        initData();
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void initData() {
        resultListBeen.clear();
        SysUserVO sysUserVO = LoginManager.getUserInfo(this);
        String uuid = sysUserVO.getUuid();
        HttpUtils.getInstance().setAuthToken(LoginManager.getToken(this));
        Call<BaseResp<List<AdultItemDTO>>> call = HttpUtils.getInstance().getApiService(RequstCommonApi.class).loadAdultList("ANDROID", uuid);
        call.enqueue(new BaseCallBack<BaseResp<List<AdultItemDTO>>>() {
            @Override
            public void onSuccess(Response<BaseResp<List<AdultItemDTO>>> response) throws Exception {
                List<AdultItemDTO> list = response.body().getData();
                System.out.println("=====list"+list.toString());
                resultListBeen.clear();
                for (AdultItemDTO item : list) {
                    if (item.getTheme().indexOf("申请人:"+sysUserVO.getUsername())>-1){

                    }else{
                        resultListBeen.add(item);
                    }
                }
                if(resultListBeen.size()==0){
                    applyforLv.setVisibility(View.GONE);
                    ivBackground.setVisibility(View.VISIBLE);
                }
                adapter = new TaskListActivityAdapter(resultListBeen, AdultListActivity.this);
                applyforLv.setAdapter(adapter);
                adapter.notifyDataSetChanged();
                applyforLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent intent = new Intent(AdultListActivity.this, AdultFromActivity.class);
                        intent.putExtra("forkey", resultListBeen.get(position).getFormKey());
                        intent.putExtra("type","审核人");
                        startActivityForResult(intent, 1);
                    }
                });

            }

            @Override
            public void onFailure(String message) {
                Toast.makeText(AdultListActivity.this,message,Toast.LENGTH_LONG).show();
            }
        });
    }


}
